package com.tmc.dao.impl.inthotel;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.tmc.bean.inthotel.hotel.CityLocation;
import com.tmc.dao.impl.BaseDaoImpl;
import com.tmc.util.ObjectUtil;

@Repository
public class CityLocationDaoImpl extends BaseDaoImpl{

	public List<CityLocation> findAll(){
		Session session = getSession();
		String hql = "from CityLocation c where c.enabledFlag = 'Y' ";
		Query query = session.createQuery(hql);
		List<CityLocation> list =(List<CityLocation>)query.list();
		return list;
	}
	
	public List<CityLocation> findAllCityLocation(CityLocation cityLocation){
		StringBuilder sql = new StringBuilder();
    	sql.append(" from CityLocation c where c.enabledFlag = 'Y' ");
    	if(ObjectUtil.hasText(cityLocation.getCityName())){
    	    Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    	    Matcher m = p.matcher(cityLocation.getCityName());
    	    if(m.find()){
    			sql.append(" and c.cityNameCN =:cityName ");
    		}else{
    			sql.append(" and c.cityName =:cityName ");
    		}
    	}
    	if(ObjectUtil.hasText(cityLocation.getLocation())){
    		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(cityLocation.getLocation());
    		if(m.find()){
    			sql.append(" and c.location =:cityLocation ");
    		}else{
    			sql.append(" and c.location =:cityLocation ");
    		}
    	}
    	Query query = getSession().createQuery(sql.toString());
    	if(ObjectUtil.hasText(cityLocation.getCityName())){
    		query.setString("cityName", cityLocation.getCityName());
    	}
    	if(ObjectUtil.hasText(cityLocation.getLocation())){
    		query.setString("cityLocation", cityLocation.getLocation());
    	}
    	return query.list();
	}
}
